package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Comment;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class CommentDao {
	private DBHelp<Comment> db = new DBHelp<Comment>();
	
	public List<Comment> findByDocumentId(String id){
		String sql = "select id,content,createtime,t_idea_id,t_message_id,t_document_id,t_user_id from t_comment where t_document_id = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new CommentRowMapper(),id);
	}
	
	public List<Comment> findByIdeaId(String id){
		String sql = "select id,content,createtime,t_idea_id,t_message_id,t_document_id,t_user_id from t_comment where t_idea_id = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new CommentRowMapper(),id);
	}
	
	/*public List<Comment> findByidea(String ideaid){
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT tc.id,tc.content,tc.createtime AS tctime,tc.t_idea_id,tc.t_user_id,ti.id,ti.title,ti.content,ti.t_user_id,ti.createtime,tu.id,tu.username,tu.password,tu.imgname FROM t_comment AS tc ");
		sql.append("LEFT JOIN t_user AS tu ON tu.id=tc.t_user_id ");
		sql.append("LEFT JOIN t_idea AS ti ON ti.id=tc.t_idea_id ");
		sql.append("WHERE tc.t_idea_id=?");
		return db.executeQueryForList(sql.toString(), new CommentRowMapper(), ideaid);
	}*/
	private class CommentRowMapper implements RowMapper<Comment>{

		public Comment mapRow(ResultSet rs) throws SQLException {
			Comment comment = new Comment();
			comment.setContent(rs.getString("content"));
			comment.setCreatetime(rs.getString("createtime"));
			comment.setDocumentid(rs.getString("t_document_id"));
			comment.setIdeaid(rs.getString("t_idea_id"));
			comment.setMessageid(rs.getString("t_message_id"));
			comment.setUserid(rs.getString("t_user_id"));
			comment.setId(rs.getString("id"));
			String id = rs.getString("t_user_id");
			UserDao dao = new UserDao();
			User user = dao.findById(id);
			comment.setUser(user);
			return comment;
		}
		
	}
	public void save(Comment comment) {
		String sql = "insert into t_comment(id,content,createtime,t_message_id,t_document_id,t_user_id ) values(?,?,?,?,?,?)";
		db.executeSQL(sql, comment.getId(),comment.getContent(),comment.getCreatetime(),comment.getMessageid(),comment.getDocumentid(),comment.getUserid());
		
	}
	public void saveInfor(Comment comment) {
		String sql = "insert into t_comment(id,content,createtime,t_message_id,t_user_id ) values(?,?,?,?,?)";
		db.executeSQL(sql, comment.getId(),comment.getContent(),comment.getCreatetime(),comment.getMessageid(),comment.getUserid());
		
	}
	public void saveIdea(Comment comment) {
		String sql = "insert into t_comment(id,content,createtime,t_idea_id,t_message_id,t_user_id ) values(?,?,?,?,?,?)";
		db.executeSQL(sql, comment.getId(),comment.getContent(),comment.getCreatetime(),comment.getIdeaid(),comment.getMessageid(),comment.getUserid());
		
	}
	public boolean del(String id){
		String sql="delete from t_comment where t_idea_id=?";
		return db.executeSQL(sql, id);
	}
	public List<Comment> findByMessageId(String id){
		String sql = "select id,content,createtime,t_idea_id,t_message_id,t_document_id,t_user_id from t_comment where t_message_id = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new CommentRowMapper(),id);
	}

	public void delByDocumentId(String id) {
		String sql="delete from t_comment where t_document_id=?";
		db.executeSQL(sql, id);
		
	}
}
